package com.dolphin.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dolphin.common.constant.CommonStatusEnum;
import com.dolphin.common.constant.DriverCarConstants;
import com.dolphin.common.dto.*;
import com.dolphin.common.response.OrderDriverResponse;
import com.dolphin.servicedriveruser.mapper.CarMapper;
import com.dolphin.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.dolphin.servicedriveruser.mapper.DriverUserMapper;
import com.dolphin.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper;
    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;
    @Autowired
    private CarMapper carMapper;

    public ResponseResult testGetDriverUser() {
        DriverUser driverUser = driverUserMapper.selectById(1);
        return ResponseResult.success(driverUser);
    }

    public ResponseResult addDriverUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);

        driverUserMapper.insert(driverUser);

        //初始化司机工作状态
        log.info("注册用户成功，用户主键id=" +driverUser.getId());
        log.info("开始设置用户默认工作状态");
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);

        return ResponseResult.success();
    }

    public ResponseResult updateDriverUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);

        driverUserMapper.updateById(driverUser);

        return ResponseResult.success();
    }

    public ResponseResult<DriverUser> getDriverUserByPhone(String driverPhone) {
        LambdaQueryWrapper <DriverUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverUser::getDriverPhone,driverPhone);
        queryWrapper.eq(DriverUser::getState, DriverCarConstants.DRIVER_STATE_VALID);
        List<DriverUser> driverUsers = driverUserMapper.selectList(queryWrapper);
        if (driverUsers.isEmpty()) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_NOT_EXISTS.getValue());
        }
        DriverUser driverUser = driverUsers.get(0);
        return ResponseResult.success(driverUser);
    }

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;
    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId) {
        // 车辆和司机绑定关系查询
        LambdaQueryWrapper <DriverCarBindingRelationship> driverCarBindingRelationshipLambdaQueryWrapper = new LambdaQueryWrapper<>();
        driverCarBindingRelationshipLambdaQueryWrapper.eq(DriverCarBindingRelationship::getCarId,carId);
        driverCarBindingRelationshipLambdaQueryWrapper.eq(DriverCarBindingRelationship::getBindState,DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(driverCarBindingRelationshipLambdaQueryWrapper);
        Long driverId = driverCarBindingRelationship.getDriverId();

        LambdaQueryWrapper<DriverUserWorkStatus> driverUserWorkStatusLambdaQueryWrapper = new LambdaQueryWrapper<>();
        driverUserWorkStatusLambdaQueryWrapper.eq(DriverUserWorkStatus::getDriverId,driverId);
        driverUserWorkStatusLambdaQueryWrapper.eq(DriverUserWorkStatus::getWorkStatus, DriverCarConstants.DRIVER_WORK_STATUS_START);

        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(driverUserWorkStatusLambdaQueryWrapper);
        if (null == driverUserWorkStatus) {
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        } else {
            //查询司机信息
            LambdaQueryWrapper <DriverUser> driverUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            driverUserLambdaQueryWrapper.eq(DriverUser::getId,driverId);
            DriverUser driverUser = driverUserMapper.selectOne(driverUserLambdaQueryWrapper);

            //查询车辆信息
            LambdaQueryWrapper<Car> carLambdaQueryWrapper = new LambdaQueryWrapper<>();
            carLambdaQueryWrapper.eq(Car::getId,carId);
            Car car = carMapper.selectOne(carLambdaQueryWrapper);

            OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
            orderDriverResponse.setDriverId(driverUser.getId());
            orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());
            orderDriverResponse.setCarId(carId);

            orderDriverResponse.setLicenseId(driverUser.getLicenseId());
            orderDriverResponse.setVehicleNo(car.getVehicleNo());
            orderDriverResponse.setVehicleType(car.getVehicleType());

            return ResponseResult.success(orderDriverResponse);
        }

    }

}
